package DAO;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class WarningDao {

	public WarningDao() {
		try {
			Class.forName("com.mysql.jdbc.Driver");

		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}

	DepositDao depositDao = new DepositDao();
	FineDao fineDao = new FineDao();

	String url = "jdbc:mysql://localhost:3306/QUESTUDY";
	String user = "root";
	String pass = "webclass";

	//String pass = "webclass";


	// study 생성할 때 생성한 사람 study 가입
	public void insertStudyFine(int sid, int uid) throws Exception {
		Connection con = null;
		PreparedStatement pstmt = null;
		try {

			con = DriverManager.getConnection(url, user, pass);

			int depositValue = depositDao.getDepositValue(sid);

			int fineValue = fineDao.getFineValue(sid);
			pstmt = con
					.prepareStatement("insert into warning(sid, uid, deposit, fine, penalty) values(?,?,?,?,1)");

			pstmt.setInt(1, sid);
			pstmt.setInt(2, uid);
			pstmt.setInt(3, depositValue);
			pstmt.setInt(4, fineValue);
			pstmt.executeUpdate();

		} catch (Exception ex) {
			ex.printStackTrace();

		} finally {
			if (pstmt != null)
				try {
					pstmt.close();
				} catch (SQLException ex) {
				}
			if (con != null)
				try {
					con.close();
				} catch (SQLException ex) {
				}
		}

	}

	// 스터디 내 벌금 낸 횟수
	public int getTheNumberOfStudyFine(int sid, int uid) throws Exception {
		Connection con = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		int count = 0;
		try {
			con = DriverManager.getConnection(url, user, pass);
			pstmt = con
					.prepareStatement("select count(*) count from warning where sid=? and uid=?");

			pstmt.setInt(1, sid);
			pstmt.setInt(2, uid);
			rs = pstmt.executeQuery();

			if (rs.next()) {
				count = rs.getInt(1);
			}

			return count;

		} catch (Exception ex) {
			ex.printStackTrace();
			return count;

		} finally {
			if (pstmt != null)
				try {
					pstmt.close();
				} catch (SQLException ex) {
				}
			if (con != null)
				try {
					con.close();
				} catch (SQLException ex) {
				}
		}

	}

}